package ext.acae2.dms.load;

import ext.acae2.utilities.util.CSCUtil;
import wt.doc.DocumentMaster;
import wt.doc.WTDocumentMaster;
import wt.epm.EPMDocument;
import wt.epm.EPMDocumentMaster;
import wt.epm.build.EPMBuildHistory;
import wt.epm.build.EPMBuildRule;
import wt.epm.structure.EPMMemberLink;
import wt.epm.structure.EPMReferenceLink;
import wt.fc.ObjectIdentifier;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.vc.VersionControlHelper;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-5-3 上午11:16:01
 */
public class DmsLoadSearchHelper {
	
	/**
	 * 
	 * @description searchWTPartUsageLink
	 * @author cnie
	 * @date 2013-6-25 上午10:57:08
	 * @version 1.0
	 * @param part
	 * @param mastered
	 * @return
	 */
	public static WTPartUsageLink searchWTPartUsageLink(WTPart part, WTPartMaster mastered) {
        try {
            QuerySpec qs = new QuerySpec(WTPartUsageLink.class);
            qs.appendWhere(new SearchCondition(WTPartUsageLink.class, "roleAObjectRef.key.id", "=", PersistenceHelper
                    .getObjectIdentifier(part).getId()), new int[1]);
            qs.appendAnd();
            qs.appendWhere(new SearchCondition(WTPartUsageLink.class, "roleBObjectRef.key.id", "=", PersistenceHelper
                    .getObjectIdentifier(mastered).getId()), new int[1]);

            qs.setAdvancedQueryEnabled(true);

            QueryResult qr = PersistenceHelper.manager.find(qs);
            if (qr.size() > 0)
                return (WTPartUsageLink) qr.nextElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	/**
	 * 
	 * @description searchEPMReferenceLink
	 * @author cnie
	 * @date 2013-6-25 上午10:56:39
	 * @version 1.0
	 * @param epm
	 * @param mastered
	 * @param identifier
	 * @return
	 */
	public static EPMReferenceLink searchEPMReferenceLink(EPMDocument epm, DocumentMaster mastered) {
        if ((!(mastered instanceof WTDocumentMaster)) && (!(mastered instanceof EPMDocumentMaster)))
            return null;
        try {
            QuerySpec qs = new QuerySpec(EPMReferenceLink.class);
            qs.appendWhere(new SearchCondition(EPMReferenceLink.class, "roleAObjectRef.key.id", "=", PersistenceHelper
                    .getObjectIdentifier(epm).getId()), new int[1]);
            qs.appendAnd();
            if ((mastered instanceof WTDocumentMaster))
                qs.appendWhere(new SearchCondition(EPMReferenceLink.class, "roleBObjectRef.key.id", "=",
                        PersistenceHelper.getObjectIdentifier((WTDocumentMaster) mastered).getId()), new int[1]);
            else if ((mastered instanceof EPMDocumentMaster)) {
                qs.appendWhere(new SearchCondition(EPMReferenceLink.class, "roleBObjectRef.key.id", "=",
                        PersistenceHelper.getObjectIdentifier((EPMDocumentMaster) mastered).getId()), new int[1]);
            }
            qs.setAdvancedQueryEnabled(true);
            QueryResult qr = PersistenceHelper.manager.find(qs);
            if (qr.size() > 0)
                return (EPMReferenceLink) qr.nextElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	/**
	 * 
	 * @description searchEPMMemberLink
	 * @author cnie
	 * @date 2013-6-25 上午10:56:39
	 * @version 1.0
	 * @param epm
	 * @param mastered
	 * @param identifier
	 * @return
	 */
	public static EPMMemberLink searchEPMMemberLink(EPMDocument epm, EPMDocumentMaster mastered, int identifier) {
        try {
            QuerySpec qs = new QuerySpec(EPMMemberLink.class);
            qs.appendWhere(new SearchCondition(EPMMemberLink.class, "roleAObjectRef.key.id", "=", PersistenceHelper
                    .getObjectIdentifier(epm).getId()), new int[1]);
            qs.appendAnd();
            qs.appendWhere(new SearchCondition(EPMMemberLink.class, "roleBObjectRef.key.id", "=", PersistenceHelper
                    .getObjectIdentifier(mastered).getId()), new int[1]);
//            qs.appendAnd();
//            qs.appendWhere(new SearchCondition(EPMMemberLink.class, "identifier", "=", identifier), new int[1]);

            qs.setAdvancedQueryEnabled(true);
            QueryResult qr = PersistenceHelper.manager.find(qs);
            if (qr.size() > 0)
                return (EPMMemberLink) qr.nextElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	/**
	 * 
	 * @description search epmbuildRule
	 * @author cnie
	 * @date 2013-6-25 上午10:55:59
	 * @version 1.0
	 * @param epm
	 * @param part
	 * @return
	 */
	public static EPMBuildRule searchEPMBuildRule(EPMDocument epm, WTPart part) {
        try {
            QuerySpec qs = new QuerySpec(EPMBuildRule.class);
            qs.appendWhere(new SearchCondition(EPMBuildRule.class, "roleAObjectRef.key.branchId", "=",
                    VersionControlHelper.getBranchIdentifier(epm)), new int[1]);
            qs.appendAnd();
            qs.appendWhere(new SearchCondition(EPMBuildRule.class, "roleBObjectRef.key.branchId", "=",
                    VersionControlHelper.getBranchIdentifier(part)), new int[1]);

            qs.setAdvancedQueryEnabled(true);
            QueryResult qr = PersistenceHelper.manager.find(qs);
            if (qr.size() > 0)
                return (EPMBuildRule) qr.nextElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	/**
	 * 
	 * @description search epmBuildHistory 
	 * @author cnie
	 * @date 2013-6-25 上午10:55:41
	 * @version 1.0
	 * @param epm
	 * @param part
	 * @return
	 */
	public static EPMBuildHistory searchEPMBuildHistory(EPMDocument epm,
			WTPart part) {
		;
		try {
			QuerySpec qs = new QuerySpec(EPMBuildHistory.class);
			qs.appendWhere(new SearchCondition(EPMBuildHistory.class,
					EPMBuildHistory.ROLE_AOBJECT_REF + "."
							+ ObjectReference.KEY + "." + ObjectIdentifier.ID,
					SearchCondition.EQUAL, PersistenceHelper
							.getObjectIdentifier(epm).getId()), new int[1]);
			qs.appendAnd();
			qs.appendWhere(new SearchCondition(EPMBuildHistory.class,
					EPMBuildHistory.ROLE_BOBJECT_REF + "."
							+ ObjectReference.KEY + "." + ObjectIdentifier.ID,
					SearchCondition.EQUAL, PersistenceHelper
							.getObjectIdentifier(part).getId()), new int[1]);

			qs.setAdvancedQueryEnabled(true);
			QueryResult qr = PersistenceHelper.manager.find(qs);
			if (qr.size() > 0)
				return (EPMBuildHistory) qr.nextElement();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) throws WTException {
		EPMDocument epm = (EPMDocument) CSCUtil.getObjectByOid("wt.epm.EPMDocument:53712");
		WTPart part = (WTPart) CSCUtil.getObjectByOid("wt.part.WTPart:51177");
		EPMBuildHistory his = searchEPMBuildHistory(epm, part);
		System.out.println(his);
	}

}
